<?
class Model_controlpanel extends Model {
    private $error_type_array = array();

    function Model_controlpanel(){
        parent::Model();
        $this->setErrorTypeArray();
    }

    function setErrorTypeArray(){
        $this->error_type_array['chkErrors'] = 1;
        $this->error_type_array['chkWarnings'] = 2;
        $this->error_type_array['chkNotice'] = 3;
        $this->error_type_array['chkUnknown'] = 4;
        $this->error_type_array['chkStrict'] = 5;
    }

    function addDomain($domain){
    	if(!$this->getDomain($domain)){
	    	$date = date("Y-m-d H:i:s");
	    	$action = "INSERT INTO domains (domain_name, date_created) VALUES(".$this->db->escape($domain).", " .$this->db->escape($date). ")";
	    	return $this->db->simple_query($action);
    	}else{
    		return false;
    	}
    }
    
    function getDomains(){
    	$action = "SELECT * FROM domains ORDER BY domain_name";
    	$result = $this->db->query($action);
    	return $result->result_array();
    }
    
    function getDomain($domain){
    	$action = "SELECT * FROM domains where domain_name = " . $this->db->escape($domain);
    	$result = $this->db->query($action);
    	
    	if ($result->num_rows() > 0){
            $row = $result->row_array();
            return $row;
        }else{
            return false;
        }
    }
    
    function getMessages($domain, $page, $records, $error_type_array=null, $timestamp=null){
        $limit = 'LIMIT ' .($page - 1) * $records .',' .$records;
        if($error_type_array === null){
            return false;
        }

        foreach($error_type_array as $error_type){
            $et[] = $this->error_type_array[$error_type];
        }

        $action = "SELECT *, m.id as message_id FROM messages m INNER JOIN message_types mt ON m.message_type = mt.id WHERE domain=" . 
                   $this->db->escape($domain) . " AND message_type IN (" . implode(",", $et) . ") AND date_created <= " . $this->db->escape($timestamp) . " ORDER BY date_created DESC $limit";
     
    	$result = $this->db->query($action);
    	return $result->result_array();
    }

    function getMessageTotals($domain, $error_type_array, $timestamp){
        foreach($error_type_array as $error_type){
            $et[] = $this->error_type_array[$error_type];
        }
        
        $action = "SELECT count(*) as total FROM messages WHERE domain=" . $this->db->escape($domain) . " AND message_type IN (". implode(",", $et) . ")" .
                  " AND date_created <= " . $this->db->escape($timestamp);
        $result = $this->db->query($action);
    	return $result->row_array();
    }

    function deleteMessage($id){
        $action = "DELETE FROM messages where id=".$this->db->escape($id);
        $result = $this->db->query($action);
    	return $result;
    }

    function clearMessages($domain){
        $action = "DELETE FROM messages WHERE domain = " . $this->db->escape($domain);
        $result = $this->db->query($action);
        return $result;
    }

    function getApplicationKey(){
        $action = "SELECT appkey FROM config LIMIT 0,1";
        $result = $this->db->query($action);
    	return $result->row_array();
    }

    function getTotalByType($domain, $type, $start=null, $end=null, $timestamp=null){
        if(!$start){
            $action = "SELECT count(*) as total FROM messages WHERE domain = " . $this->db->escape($domain) . " AND message_type = $type";
            if($timestamp !== null){
                $action .= " AND date_created <= " . $this->db->escape($timestamp);
            }
        }else{
            $action = "SELECT count(*) as total FROM messages WHERE domain = " . $this->db->escape($domain) . " AND message_type = $type
                      AND date_created >= '$start' AND date_created < '$end'";
        }

        $result = $this->db->query($action);
    	$total_array = $result->row_array();
        return $total_array['total'];
    }

    function getMostOccurringUrls($domain_name, $start=null, $end=null){
        if(!$start){
            $action = "SELECT count(*) as total, php_self FROM messages WHERE domain=" . $this->db->escape($domain_name) . " group by php_self order by total desc limit 0, 10";
        }else{
            $action = "SELECT count(*) as total, php_self FROM messages WHERE domain=" . $this->db->escape($domain_name) .
                      " AND date_created >= '$start' AND date_created < '$end' group by php_self order by total desc limit 0, 10";
        }
  
        $result = $this->db->query($action);
    	return $result->result_array();
    }
}

?>